/**
 * Created by lx on 2017/4/17.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var customer = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            customer = JSON.parse(has);
            $('#initial').hide() ;
            $('#edit').show() ;
            var template = {} ;
            template.id = customer.templateId ;
            $.ajax({
                type: "POST",
                url: "/template/listTagBlock",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(template),
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        var content = "" ;
                        $.each(data.param.list,function(i,val){
                            content+=createcollaItem(val) ;
                        }) ;
                        $('#edit').empty() ;
                        $('#edit').append(content) ;
                        form.render() ;
                        $.ajax({
                            type: "POST",
                            url: "/customer/get",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(customer),
                            dataType: "json",
                            success: function (data) {
                                if (data.success) {
                                    $.each(data.param.customer.customerInfoDOs,function(i,val){
                                        $('.layui-tag#'+val.tagLabelId).val(val.value) ;
                                    }) ;
                                } else {
                                    layer.msg("获取客户错误");
                                }
                            },
                            error: function (message) {
                                layer.msg("获取客户错误");
                            }
                        });
                    } else {
                        layer.msg("获取客户错误");
                    }
                },
                error: function (message) {
                    layer.msg("获取标签块错误");
                }
            });
        }else{
            $('#initial').show() ;
            $('#edit').hide() ;
            $.ajax({
                type: "POST",
                url: "/template/list?pageNum=1&pageSize=1000",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.success) {
                        var content = "" ;
                        content += '<option value=""></option>' ;
                        $.each(data.param.list,function(i,val){
                            content += '<option value="'+ val.id + '">' + val.name + '</option>' ;
                        }) ;
                        $('#templateId').empty() ;
                        $('#templateId').append(content) ;
                        form.render() ;
                    } else {
                        layer.msg("获取模板错误");
                    }
                },
                error: function (message) {
                    layer.msg("获取模板错误");
                }
            });
        }
        autoInject(customer);
        form.on('submit(yes)', function (data) {
            var url;
            if (customer.id == undefined) {
                url = "/customer/add"
                customer = autoFetch(customer);
                if(!isNotNull(customer.templateId)){
                    layer.msg("请选择一个模板");
                    return false ;
                }
            } else {
                url = "/customer/update";
                customer.customerInfoDOs = [] ;
                $('.layui-tag').each(function(index,item){
                    customer.customerInfoDOs.push({customerId:customer.id,tagLabelId:item.id,value:item.value}) ;
                }) ;
            }
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(customer),
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.msg("新增客户成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    } else {
                        layer.msg("新增客户失败");
                    }
                },
                error: function (message) {
                    layer.msg("新增客户失败");
                }
            });
            return false;
        });

        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false;
        });
    });
})
;